/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addFrame.java
 *
 * Created on 2011. 2. 3, pm 3:29:39
 */
/**
 *
 * @author Gi-Hoon
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {

 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGap(0, 400, Short.MAX_VALUE)
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGap(0, 300, Short.MAX_VALUE)
 );

 pack();
 }// </editor-fold>
 */
// Variables declaration - do not modify
// End of variables declaration
package todomanager.View;

/**
 *
 * @author Gi-Hoon
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ResourceBundle;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import todomanager.data.ITask;
import todomanager.data.StandardTask;
import todomanager.io.ITodoImporter;
import todomanager.io.TodoXMLReader;

public class EditFrame extends JFrame implements ChangeListener, ActionListener {

	private ResourceBundle language;

	private JLabel lName = new JLabel();
	private JLabel lDescription = new JLabel();
	private JLabel lCategory = new JLabel();
	private JLabel lDate = new JLabel();
	private JLabel lDate_form = new JLabel();
	private JLabel lPriority = new JLabel();
	private JLabel lProgress = new JLabel();
	// private JTextField textName = new JTextField();
	private JComboBox comboName = new JComboBox();
	private JTextArea textDescription = new JTextArea();
	private JComboBox comboCategory = new JComboBox();
	private JTextField textDate = new JTextField();
	private JComboBox comboPriority = new JComboBox();
	private JComboBox comboProgress = new JComboBox();
	private JButton buttonOk;
	private JButton buttonCancel;
	private JPanel panelLabel = new JPanel(new GridLayout(0, 1));
	private JPanel panelDate = new JPanel(new GridLayout(0, 1));
	private JPanel panelComponent = new JPanel(new GridLayout(0, 1));
	private JPanel panelButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));

	private String vName = "name";
	private String vDescription = "description";
	private String vCategory;
	private String vDate = "date";
	private String vPriority;
	private int vProgress = 0;

	private TitledBorder titleName;
	private TitledBorder titleDescription;
	private TitledBorder titlePriority;
	private TitledBorder titleDate;
	private TitledBorder titleProgress;
	private TitledBorder titleCategory;

	private int valProgress;
	private JSlider Progress = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);

	private ArrayList nameList;
	private ArrayList dateList;
	private ArrayList descriptionList;
	private ArrayList categoryList;
	private ArrayList priorityList;
	private ArrayList progressList;

	private ArrayList<String> editItem;

	private String str;
	private Integer help = -1;

	private File databaseFile;
	private AddFrameView addView;

	public String getLabel(String labelName) {
		return this.language.getString(labelName);
	}

	public EditFrame(ResourceBundle lang) {
		try {
			this.language = lang;
			Aframe();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public EditFrame(ResourceBundle lang, String name, String des, String cate,
			String date, String prio, int pro) {
		try {
			this.language = lang;
			this.vName = name;
			this.vDescription = des;
			this.vCategory = cate;
			this.vDate = date;
			this.vPriority = prio;
			this.vProgress = pro;
			Aframe();
			// showItemFromComboBox();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void Aframe() throws Exception {
		this.setSize(new Dimension(400, 300));

		this.setLayout(new BorderLayout());
		/*
		 * lName.setText(this.getLabel("taskNameLabel")); lName.setBounds(new
		 * Rectangle(40, 15));
		 * 
		 * lDescription.setText(this.getLabel("taskDescriptionLabel"));
		 * lDescription.setBounds(new Rectangle(70, 15));
		 * 
		 * lCategory.setText(this.getLabel("taskCategoryLabel"));
		 * lCategory.setBounds(new Rectangle(65, 15));
		 * 
		 * lDate.setText(this.getLabel("taskDateLabel")); lDate.setBounds(new
		 * Rectangle(40, 15)); lDate_form.setText("(yyyy-mm-dd)");
		 * lDate_form.setBounds(new Rectangle(80,15));
		 * 
		 * lPriority.setText(this.getLabel("taskPriorityLabel"));
		 * lPriority.setBounds(new Rectangle(40, 15));
		 * 
		 * lProgress.setText("Progress"); lProgress.setBounds(new
		 * Rectangle(65,15));
		 */
		// textName.setBounds(new Rectangle(200, 20));\
		comboName.setBounds(new Rectangle(200, 20));

		textDescription.setBounds(new Rectangle(200, 50));

		comboCategory.setBounds(new Rectangle(200, 20));
		comboCategory.addItem(this.getLabel("taskCategoryAssignment"));
		comboCategory.addItem(this.getLabel("taskCategoryMeeting"));
		comboCategory.addItem(this.getLabel("taskCategoryClass"));

		// TODO: get rid of below?
		comboCategory.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Combo_category_actionPerformed(e);
			}
		});

		textDate.setBounds(new Rectangle(200, 20));

		comboPriority.setBounds(new Rectangle(200, 20));
		comboPriority.addItem(this.getLabel("taskPriorityLow"));
		comboPriority.addItem(this.getLabel("taskPriorityNormal"));
		comboPriority.addItem(this.getLabel("taskPriorityHigh"));
		comboPriority.addItem(this.getLabel("taskPriorityVeryHigh"));

		// ------------------------------------------------------------------------
		databaseFile = new File("database.xml");
		if (databaseFile.exists()) {
			ITodoImporter read = new TodoXMLReader(new BufferedReader(
					new FileReader("database.xml")));
			nameList = read.importName();
			System.out.println("the list is:" + nameList);
			Iterator tli = nameList.iterator();
			while (tli.hasNext()) {
				comboName.addItem(tli.next());
			}
		}
		comboName.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent ie) {
				help = new Integer(comboName.getSelectedIndex());

				// -------------------------Description------------------------------
				databaseFile = new File("database.xml");
				if (databaseFile.exists()) {
					ITodoImporter readDescription = null;
					try {
						readDescription = new TodoXMLReader(new BufferedReader(
								new FileReader("database.xml")));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						descriptionList = readDescription.importDescription();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Object descriptionArray[] = descriptionList.toArray();
					if (help == -1) {
						textDescription.setText("Date");
					} else {
						String date = (String) descriptionArray[help];
						textDescription.setText(date);
					}
				}
				// -----------------------Date-----------------------------------
				databaseFile = new File("database.xml");
				if (databaseFile.exists()) {
					ITodoImporter read = null;
					try {
						read = new TodoXMLReader(new BufferedReader(
								new FileReader("database.xml")));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						dateList = read.importDate();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Object dateArray[] = dateList.toArray();
					if (help == -1) {
						textDate.setText("Date");
					} else {
						String date = (String) dateArray[help];
						textDate.setText(date);
					}
				}
				// ------------------------Category----------------------------
				databaseFile = new File("database.xml");
				if (databaseFile.exists()) {
					ITodoImporter read = null;
					try {
						read = new TodoXMLReader(new BufferedReader(
								new FileReader("database.xml")));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						categoryList = read.importCategory();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Object[] categoryArray = categoryList.toArray();
					if (help == -1) {
						comboCategory.setSelectedItem(vCategory);
					} else {
						String helpString = (String) categoryArray[help];
						if (helpString.equals("Assignment")) {
							String date = "Assignment";
							comboCategory.setSelectedItem(date);
						} else if (helpString.equals("Meeting")) {
							String date = "Meeting";
							comboCategory.setSelectedItem(date);
						} else if (helpString.equals("Class")) {
							String date = "Class";
							comboCategory.setSelectedItem(date);
						}
					}
				}
				// -----------------------------priority------------------------------------
				databaseFile = new File("database.xml");
				if (databaseFile.exists()) {
					ITodoImporter read = null;
					try {
						read = new TodoXMLReader(new BufferedReader(
								new FileReader("database.xml")));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						priorityList = read.importPriority();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Object[] priorityArray = priorityList.toArray();
					if (help == -1) {
						comboPriority.setSelectedItem(vPriority);
					} else {
						int cat = (Integer) priorityArray[help];
						System.out.println("cat is:" + cat);
						if (cat == 0) {
							String date = "Low";
							comboPriority.setSelectedItem(date);
						} else if (cat == 1) {
							String date = "Normal";
							comboPriority.setSelectedItem(date);
						} else if (cat == 2) {
							String date = "High";
							comboPriority.setSelectedItem(date);
						} else if (cat == 3) {
							String date = "Very High";
							comboPriority.setSelectedItem(date);
						}
					}
				}
				// --------------------------------Progress------------------------------------
				databaseFile = new File("database.xml");
				if (databaseFile.exists()) {
					ITodoImporter read = null;
					try {
						read = new TodoXMLReader(new BufferedReader(
								new FileReader("database.xml")));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						progressList = read.importProgress();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Object[] progressArray = progressList.toArray();
					if (help == -1) {
						Progress.setValue(vProgress);
					} else {
						int date = (Integer) progressArray[help];
						Progress.setValue(date);
					}
				}
				// --------------------------------End of
				// Progress-----------------------------
			}
		});

		textDate.setText(this.getLabel("taskDateLabel"));

		// Progress Slider

		Progress.setMajorTickSpacing(10);
		Progress.setMinorTickSpacing(5);
		Progress.setPaintTicks(true);
		Progress.setPaintLabels(true);
		Progress.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		Progress.addChangeListener(this);

		comboProgress.setBounds(new Rectangle(200, 20));
		comboProgress.addItem("0");
		comboProgress.addItem("1");
		comboProgress.addItem("2");
		comboProgress.addItem("3");
		comboProgress.addItem("4");
		comboProgress.addItem("5");
		comboProgress.addItem("6");
		comboProgress.addItem("7");
		comboProgress.addItem("8");
		comboProgress.addItem("9");
		comboProgress.addItem("10");

		// set border.
		titleName = BorderFactory.createTitledBorder(this.getLabel("taskNameLabel"));
		titleDescription = BorderFactory.createTitledBorder(this.getLabel("taskDescriptionLabel"));
		titleDate = BorderFactory.createTitledBorder(this.getLabel("taskDateLabel")+"(yyyy-mm-dd)");
		titleCategory = BorderFactory.createTitledBorder(this.getLabel("taskCategoryLabel"));
		titlePriority = BorderFactory.createTitledBorder(this.getLabel("taskPriorityLabel"));
		titleProgress = BorderFactory.createTitledBorder(this.getLabel("taskProgressLable"));

		comboName.setBorder(titleName);
		textDescription.setBorder(titleDescription);
		textDate.setBorder(titleDate);
		comboCategory.setBorder(titleCategory);
		comboPriority.setBorder(titlePriority);
		Progress.setBorder(titleProgress);

		comboName.setSelectedItem(vName);
		textDescription.setText(vDescription);
		textDate.setText(vDate);
		comboCategory.setSelectedItem(vCategory);
		comboPriority.setSelectedItem(vPriority);
		Progress.setValue(vProgress);

		/*
		 * panelDate.add(lDate); panelDate.add(lDate_form);
		 * 
		 * 
		 * panelLabel.add(lName); panelLabel.add(lDescription);
		 * panelLabel.add(panelDate); panelLabel.add(lCategory);
		 * panelLabel.add(lPriority); panelLabel.add(lProgress);
		 */
		panelComponent.add(comboName);
		panelComponent.add(textDescription);
		panelComponent.add(textDate);
		panelComponent.add(comboCategory);
		panelComponent.add(comboPriority);
		panelComponent.add(Progress);

		this.add("South", panelButton);
		// this.add("West", panelLabel);
		this.add("Center", panelComponent);
	}

	/**
	 * 
	 * @return the inputted data from the different fields in the view.
	 */
	/*
	 * public ITask getUserInput() { return new
	 * StandardTask(comboName.getSelectedIndex(), textDescription.getText(),
	 * textDate.getText(), new
	 * Integer(comboCategory.getSelectedIndex()).toString(),
	 * comboPriority.getSelectedIndex(), val_progress, (val_progress==100? true
	 * : false)); }
	 */
	public void editButtonActions(Action editTaskAction, Action cancelAction) {
		this.buttonOk = new JButton(editTaskAction);
		this.buttonCancel = new JButton(cancelAction);

		panelButton.add(buttonOk);
		panelButton.add(buttonCancel);
	}

	/**
	 * 
	 * @param e
	 *            ActionEvent for the combobox.
	 */
	private void Combo_category_actionPerformed(ActionEvent e) {
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		JSlider source = (JSlider) e.getSource();
		if (!source.getValueIsAdjusting()) {
			valProgress = (int) source.getValue();

		}
	}

	// work on editing items
	
	/**
	 * 
	 * @return the inputted data from the different fields in the view.
	 */
	public ITask getUserInput() {
			return new StandardTask(comboName.getSelectedItem().toString(),
					textDescription.getText(), textDate.getText(), 
							comboCategory.getSelectedItem().toString(),
					comboPriority.getSelectedIndex(), valProgress,
					(valProgress == 100 ? true : false));
	}

	
	public ArrayList<String> getInput() {
		editItem=new ArrayList<String>();
		editItem.add(Integer.toString(help));
		return editItem;
	}
}
